package com.example.wujinapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wujinapi.dao.FirsttypeDao;
import com.example.wujinapi.dao.SecondtypeDao;
import com.example.wujinapi.entity.Firsttype;
import com.example.wujinapi.entity.Secondtype;
import com.example.wujinapi.service.SecondtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Secondtype)表服务实现类
 *
 * @author makejava
 * @since 2023-07-15 17:34:09
 */
@Service("secondtypeService")
public class SecondtypeServiceImpl extends ServiceImpl<SecondtypeDao, Secondtype> implements SecondtypeService {

    @Autowired
    private SecondtypeDao secondtypeDao;


    @Override
    public void addSecondtype(Secondtype secondtype) {
        Secondtype Data=secondtypeDao.selectOne(new QueryWrapper<Secondtype>().eq("secondclass", secondtype.getSecondclass()));
        if(Data==null){ //为空才插入新用户
            secondtypeDao.insert(secondtype);
        }
    }

    @Override
    public void updateFirstClass(int firstClassId,String beforName,String firstClassName) {
        //System.out.println("更新二级分类"+firstClassId+","+firstClassName);
        Secondtype fData=secondtypeDao.selectOne(new QueryWrapper<Secondtype>().eq("firstid",firstClassId).eq("firstclass",beforName));
        //System.out.println("获取二级分类对象"+fData);
        if(fData!=null){
            fData.setFirstclass(firstClassName);
            //System.out.println("更改对象后"+fData);
            secondtypeDao.updateById(fData);
        }
    }

    @Override
    public void deleteSecondtype(int dataId) {
        secondtypeDao.delete(new QueryWrapper<Secondtype>().eq("id",dataId));
    }

    @Override
    public List<Secondtype> getSecondtypes() {
        List<Secondtype> listDatas=secondtypeDao.selectList(new QueryWrapper<Secondtype>().orderByDesc("secondsortnum"));
        return listDatas!=null?listDatas:null;
    }

    @Override
    public List<Secondtype> getSecondtypesByFirstId(int firstId) {
        List<Secondtype> listDatas=secondtypeDao.selectList(new QueryWrapper<Secondtype>().eq("firstid",firstId).orderByDesc("secondsortnum"));
        return listDatas!=null?listDatas:null;
    }
}

